/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Eureka! project.
 * 
 * Eureka! is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Eureka! is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Eureka!. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.logic;

import it.rainbowbreeze.eureka.common.LogFacility;
import it.rainbowbreeze.eureka.common.ResultOperation;
import it.rainbowbreeze.eureka.data.WebserviceManager;
import it.rainbowbreeze.eureka.domain.QualityDataPoint;
import it.rainbowbreeze.libs.jobexecutor.RainbowJob;
import it.rainbowbreeze.libs.jobexecutor.RainbowJobResult;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class DownloadQualityDataJob extends RainbowJob {
    // ------------------------------------------ Private Fields
    private static final String LOG_HASH = DownloadQualityDataJob.class.getSimpleName();
    
    /** Uniquely identify the job parameters */
    private final String mJobUniqueKey;
    private final WebserviceManager mWebserviceManager;
    private final int mLatitude;
    private final int mLongitude;
    private final int mRadius;
    private final int[] mMeasureIds;
    
    // -------------------------------------------- Constructors
    public DownloadQualityDataJob(
            LogFacility logFacility,
            WebserviceManager webserviceManager,
            String jobUniqueId,
            int latitude,
            int longitude, 
            int radius,
            int[] measureIds) {
        super(logFacility);
        mWebserviceManager = webserviceManager;
        mJobUniqueKey = jobUniqueId;
        mLatitude = latitude;
        mLongitude = longitude;
        mRadius = radius;
        mMeasureIds = measureIds;
    }


    // --------------------------------------- Public Properties
    /* (non-Javadoc)
     * @see it.rainbowbreeze.libs.jobexecutor.RainbowJob#getCategoryId()
     */
    @Override
    public int getCategoryId() {
        return JobExecutor.JOB_CATEGORY_DOWNLOAD_MAP_DATA;
    }

    /* (non-Javadoc)
     * @see it.rainbowbreeze.libs.jobexecutor.RainbowJob#onlyOneExecutionForCategory()
     */
    @Override
    public boolean onlyOneExecutionForCategory() {
        return false;
    }


    // ------------------------------------------ Public Classes
    public static class Result extends RainbowJobResult<ResultOperation<QualityDataPoint[]>> {
        private final String mJobUniqueKey;
        
        public Result(
                long submissionId,
                ResultOperation<QualityDataPoint[]> resultOperation,
                String jobUniqueKey) {
            super(submissionId, resultOperation);
            mJobUniqueKey = jobUniqueKey;
        }
        
        public String getJobUniqueKey() {
            return mJobUniqueKey;
        }
    }
    
    
    // ------------------------------------------ Public Methods

    
    // ----------------------------------------- Private Methods
    /* (non-Javadoc)
     * @see it.rainbowbreeze.libs.jobexecutor.RainbowJob#getShortJobDescription()
     */
    @Override
    protected String getShortJobDescription() {
        return "Download points for [" + mLatitude + "," + mLongitude + "] radius " + mRadius;
    }

    /* (non-Javadoc)
     * @see it.rainbowbreeze.libs.jobexecutor.RainbowJob#getLogHash()
     */
    @Override
    protected String getLogHash() {
        return LOG_HASH;
    }

    /* (non-Javadoc)
     * @see it.rainbowbreeze.libs.jobexecutor.RainbowJob#executeJob()
     */
    @Override
    protected Result executeJob() {
        ResultOperation<QualityDataPoint[]> dataResult = mWebserviceManager.getDataPoints(
                mLatitude,
                mLongitude,
                mRadius,
                mMeasureIds);

        Result result = new Result(getSubmissionId(), dataResult, mJobUniqueKey);
        return result;
    }
}
